import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demo/pages/home_cart/home_cart_logic.dart';
import 'package:flutter_demo/pages/home_cart/home_cart_view.dart';
import 'package:flutter_demo/pages/home_first/home_first_logic.dart';
import 'package:flutter_demo/pages/home_first/home_first_view.dart';
import 'package:flutter_demo/pages/home_message/home_message_logic.dart';
import 'package:flutter_demo/pages/home_message/home_message_view.dart';
import 'package:flutter_demo/pages/home_profile/home_profile_logic.dart';
import 'package:flutter_demo/pages/home_profile/home_profile_view.dart';
import 'package:get/get.dart';

import '../../i18n/locale_keys.dart';
import '../../utils/svgs.dart';
import '../../widget/navigation.dart';
import 'home_logic.dart';

class HomePage extends GetView<HomeLogic> {
  HomePage({Key? key}) : super(key: key);


  @override
  Widget build(BuildContext context) {
    Get.put(Home_firstLogic());
    Get.put(Home_cartLogic());
    Get.put(Home_messageLogic());
    Get.put(Home_profileLogic());
    return GetBuilder(builder: (_){
      return _buildView(context);
    },
      init: HomeLogic(),
     id: "home",);
  }
  

  // 主视图
  Widget _buildView(BuildContext context) {
    return PopScope(
      // 允许返回
      canPop: false,

      // 防止连续点击两次退出
      onPopInvokedWithResult: (bool didPop, dynamic result) {
        // 如果返回，则不执行退出请求
        if (didPop) {
          return;
        }

        // 退出请求
        if (controller.closeOnConfirm(context)) {
          SystemNavigator.pop(); // 系统级别导航栈 退出程序
        }
      },

      child: Scaffold(
        appBar: AppBar(
          title: const Text("主页"),
        ),
        extendBody: true,
        resizeToAvoidBottomInset: false,
        // 导航栏
        bottomNavigationBar: GetBuilder<HomeLogic>(
          id: 'navigation',
          builder: (controller) {
            return BuildNavigation(
              currentIndex: controller.currentIndex,
              items: [
                NavigationItemModel(
                  label: LocaleKeys.tabBarHome.tr,
                  icon: AssetsSvgs.navHomeSvg,
                ),
                NavigationItemModel(
                  label: LocaleKeys.tabBarCart.tr,
                  icon: AssetsSvgs.navCartSvg,
                  count: 3,
                ),
                NavigationItemModel(
                  label: LocaleKeys.tabBarMessage.tr,
                  icon: AssetsSvgs.navMessageSvg,
                  count: 9,
                ),
                NavigationItemModel(
                  label: LocaleKeys.tabBarProfile.tr,
                  icon: AssetsSvgs.navProfileSvg,
                ),
              ],
              onTap: controller.onJumpToPage, // 切换tab事件
            );
          },
        ),
        // 内容页
        body: PageView(
          physics: const NeverScrollableScrollPhysics(),
          controller: controller.pageController,
          onPageChanged: controller.onIndexChanged,
          children:  [
            // 加入空页面占位
            Home_firstPage(),
            Home_cartPage(),
            Home_messagePage(),
            Home_profilePage(),
          ],
        ),
      ),
    );
  }

}
